import subprocess

def get_changed_files(keyword):
    # 获取所有提交的变动文件
    result = subprocess.run(['git', 'log', '--grep', keyword, '--name-only', '--pretty=format:'], stdout=subprocess.PIPE)
    changed_files = result.stdout.decode('utf-8').strip().split('\n')
    # 过滤掉空行并去重，只保留.c、.h和CMakeLists.txt文件
    unique_files = list(set(file for file in changed_files if file and (file.endswith('.c') or file.endswith('.h') or file == 'CMakeLists.txt')))
    return unique_files

# ... existing code ...

def record_changed_files(keyword, file_path='changed_files.txt'):
    changed_files = get_changed_files(keyword)
    
    if changed_files:  # 只有在有变动文件时才记录
        with open(file_path, 'w') as f:
            for file in changed_files:
                f.write(file + '\n')

if __name__ == "__main__":
    keyword = input("请输入关键词：")  # 从用户输入获取关键词
    record_changed_files(keyword)